<?
  class user_auth extends add_module{

   // securitu and session func----------------
    protected function _use_session($arg="",$arr=""){
     session_start;
     switch($arg){
      case("start"):{
       if(empty($_SESSION['type'])){
         session_register("count");
         session_register("type");
         $_SESSION['count']=0;
         $_SESSION['type']="unknown";
        }else{
         if ($_SESSION['type']=="unknown")$_SESSION['count']++  ;
        }
      }break;

      case("admin_mode"):{
       $tmp=$this->model->return_info_user($arr['type']);
       session_register("user");
       $_SESSION['user']=$tmp[0];
       $_SESSION['count']=0;
       $_SESSION['type']="member";
      }break;

	  case("access"):{
       if ($_SESSION['type']=="member" && $_SESSION['user']['level']>=LEVEL_ACCESS_ADMINPANEL ){
        return true;
        }else{
      	 return false;
      	 }
      }break;

      case("user_mode"):{       $tmp=$this->model->return_info_user($arr['type']);       session_register("user");
       $_SESSION['user']=$tmp[0];
       $_SESSION['count']=0;
       $_SESSION['type']="user";
      }break;

      case("access_user"):{
       if ($_SESSION['type']=="user" ){
        return true;
        }else{
      	 return false;
      	 }
      }break;

      default:{      session_register("user");
      $_SESSION['user']['level']=0;      return false;
      }break;
     }


    }

    protected function _login(){
     if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['pass'] ) && !empty($_POST['login'])) {
       if ($_SESSION['count']>=COUNT_ERR_LOGIN){
       	return $this->lang['count_error'];
        }else{
         $login=$this->security($_POST['login']);
         $pass= $this->security($_POST['pass']);
         $tmp=$this->model->return_login_users();
          foreach($tmp as $key=>$value){
           if ($value['type']==$login){
            if(md5($pass)==$this->model->return_pass($value['type'])){
               if($value['level']>LEVEL_ACCESS_ADMINPANEL){
                $this->_use_session("admin_mode",$value);
                header("location: /".ADMIN_DIR); end;
                }else{               	 $this->_use_session("user_mode",$value);
               	 header("location: /"); end;
                }

              }else{
             	return $this->lang['password_error'];
              }
           	 }
          }
          return $this->lang["login_error"];
         };
      }else{
       return "";
       };

    }


 // end securitu and session-----------------


  function login_standby(){   if ($this->_use_session("access_user"))
    return $this->_show_hello();
     else      return $this->_show_form();
  }

  function login_schedule(){  	if ($this->_use_session("access_user"))
     return $this->_show_hello();
     else
      return $this->_show_form();
  }

  function logined(){
    $this->_use_session("start");  	$data['error']=$this->_login();
   $data[$this->model->get_info("side","user_auth","events","logined")]=$this->view->view("templates/site/tpl_module/user_auth/login_form.tpl",$data,true);
    return $data;
  }

  protected function _show_form(){  	$data['error']="";
    $data=$this->view->view("templates/site/tpl_module/user_auth/login_form.tpl",$data,true);
  	return $data;
  }

  protected function _show_hello(){
     $data['level_access_l']=$_SESSION['user']['level'];
     $data['login_type']=$_SESSION['user']['type'] ;
     $data['login_name']=$_SESSION['user']['name'];
     $data=$this->view->view("templates/site/tpl_module/user_auth/login_panel.tpl", $data,true);
     return $data;
  }

   protected function _show_hello_admin(){

     $data['level_access_l']=$_SESSION['user']['level'];
     $data['login_type']=$_SESSION['user']['type'] ;
     $data['login_name']=$_SESSION['user']['name'];
     $data=$this->view->view("templates/admin/login/login_panel.tpl", $data,true);
     return $data;
  }

  function logout(){   unset($_SESSION['type']);   unset($_SESSION['user']);
   header("location: /");
  }

  protected function default_user(){   if(empty($_SESSION['user']['level']))$this->_use_session("default");
  }

}
?>